//
//  MoreViewController.m
//  EducationOID
//
//  Created by slz on 2017/5/24.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "MoreViewController.h"
#import "NewWebViewController.h"
#import "ContactUsViewController.h"
#import "IntroduceOrgViewController.h"
#import "SystemSetViewController.h"
#import "OIDHistoryViewController.h"
#import "OIDSystemSettingManager.h"
#import "OIDCacheManager.h"

@implementation MoreItemViewCell
-(instancetype)init{
    self = [super init];
    if (self){
        _titleLabel = [UILabel new];
        _titleLabel.fontSize = 14;
        _titleLabel.textColor = kDark_TextColor_Default;
        [self addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(11);
            make.centerY.mas_equalTo(self.mas_centerY);
        }];
        
        UIImageView * arrowImageView = [UIImageView new];
        [self addSubview:arrowImageView];
        arrowImageView.image = [UIImage imageNamed:@"icon_right_small_arrow"];
        [arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(12, 12));
            make.centerY.mas_equalTo(self.mas_centerY);
            make.right.mas_equalTo(self.mas_right).offset(-11);
        }];
        
        _lineView = [UIView new];
        _lineView.backgroundColor = kLineColor_Gray_Default;
        [self addSubview:_lineView];
        [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(10);
            make.right.mas_equalTo(self.mas_right).offset(-10);
            make.height.mas_equalTo(0.5);
            make.bottom.mas_equalTo(self.mas_bottom);
        }];
        
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)]];
    }
    return self;
}

- (void)tap{
    if ([self.delegate respondsToSelector:@selector(cellDidClickIndex:andGroup:)]){
        [_delegate cellDidClickIndex:_index andGroup:_groupIndex];
    }
}

@end

@interface MoreItemCellGroupView : UIView
@property(nonatomic,assign) NSInteger groupIndex;
-(instancetype)initWithGroupTitles:(NSArray *)titles andDelegate:(id)delegate;
@end

@implementation MoreItemCellGroupView

-(instancetype)initWithGroupTitles:(NSArray *)titles andDelegate:(id)delegate{
    self = [super init];
    if (self){
        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor whiteColor];
        for (int i = 0; i < titles.count; i ++) {
            NSString * title = titles[i];
            MoreItemViewCell * cell = [[MoreItemViewCell alloc]init];
            cell.index = i;
            cell.delegate = delegate;
            [self addSubview:cell];
            [cell mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.and.right.mas_equalTo(self);
                make.height.mas_equalTo(40);
                make.top.mas_equalTo(self.mas_top).offset(i * 40);
            }];
            cell.titleLabel.text = title;
            cell.lineView.hidden = i == titles.count - 1;
        }
    }
    return self;
}

-(void)setGroupIndex:(NSInteger)groupIndex{
    for (MoreItemViewCell * cell in self.subviews) {
        cell.groupIndex = groupIndex;
    }
}

@end


@interface MoreViewController ()<MoreCellDelegate>
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UIView *contentView;
@end


@implementation MoreViewController
{
    NSArray * _titles1;
    NSArray * _titles2;
    NSArray * _titles3;
    UIButton * _logoutButton;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _titles1 = @[@"解码历史记录",@"OID生成码记录",@"收藏"];
    _titles2 = @[@"系统设置",@"关于我们"];
    _titles3 = @[@"教育OID注册解析中心介绍"];

    self.showNavigationBar = YES;
    [self setControllerTitle:@"更多"];
    self.view.backgroundColor = [UIColor colorWithRGB:0xE3E3E3];
    UIBarButtonItem * item = [UIBarButtonItem itemWithImage:@"top_back_1" selectImage:nil addTarget:self andAction:@selector(backAction)];
    self.navigationItem.leftBarButtonItem = item;
    
    [self initScrollView];
    // Do any additional setup after loading the view.
}

- (void)initScrollView{
    _scrollView = [UIScrollView new];
    [self.view addSubview:_scrollView];
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    _contentView = [UIView new];
    _contentView.backgroundColor = [UIColor colorWithRGB:0xE3E3E3];
    [_scrollView addSubview:_contentView];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
        make.height.mas_equalTo(410);
    }];
    
    MoreItemCellGroupView * groupView1 = [[MoreItemCellGroupView alloc]initWithGroupTitles:_titles1 andDelegate:self];
    groupView1.groupIndex = 0;
    [_contentView addSubview:groupView1];
    [groupView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_contentView.mas_left).offset(11);
        make.top.mas_equalTo(_contentView.mas_top).offset(20);
        make.right.mas_equalTo(_contentView.mas_right).offset(-11);
        make.height.mas_equalTo(_titles1.count * 40);
    }];
    
    MoreItemCellGroupView * groupView2 = [[MoreItemCellGroupView alloc]initWithGroupTitles:_titles2 andDelegate:self];
    groupView2.groupIndex = 1;
    [_contentView addSubview:groupView2];
    [groupView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_contentView.mas_left).offset(11);
        make.top.mas_equalTo(groupView1.mas_bottom).offset(15);
        make.right.mas_equalTo(_contentView.mas_right).offset(-11);
        make.height.mas_equalTo(_titles2.count * 40);
    }];

    MoreItemCellGroupView * groupView3 = [[MoreItemCellGroupView alloc]initWithGroupTitles:_titles3 andDelegate:self];
    groupView3.groupIndex = 2;
    [_contentView addSubview:groupView3];
    [groupView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_contentView.mas_left).offset(11);
        make.top.mas_equalTo(groupView2.mas_bottom).offset(15);
        make.right.mas_equalTo(_contentView.mas_right).offset(-11);
        make.height.mas_equalTo(_titles3.count * 40);
    }];
    
    BOOL hasLogin = [MFWSdkEnvironment sharedInstance].hasLogin;
    if (hasLogin){
        _logoutButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _logoutButton.layer.cornerRadius = 5;
        _logoutButton.titleLabel.fontSize = 14;
        _logoutButton.layer.masksToBounds = YES;
        [_contentView addSubview:_logoutButton];
        _logoutButton.backgroundColor = kBack_Color_Default;
        [_logoutButton setTitle:@"退出" forState:UIControlStateNormal];
        [_logoutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_logoutButton addTarget:self action:@selector(logoutAction) forControlEvents:UIControlEventTouchUpInside];
        [_logoutButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(40);
            make.left.and.right.mas_equalTo(groupView3);
            make.top.mas_equalTo(groupView3.mas_bottom).offset(40);
        }];
    }
}

-(void)cellDidClickIndex:(NSInteger)index andGroup:(NSInteger)groupIndex{
    if (groupIndex == 0)
    {
        OIDSystemSettingManager * setManager = [OIDSystemSettingManager shareManager];
        BOOL remmenberDuplicate = setManager.remmenberDuplicate;
        OIDCacheManager * manager = [OIDCacheManager shareInstance];
        OIDHistoryViewController * hisController = [[OIDHistoryViewController alloc]init];
        if (index == 0){
            NSArray * arr = remmenberDuplicate ? [manager getAnalysisOidList] : [self arrWithDelipublic:[manager getAnalysisOidList]];
            hisController.type = OIDHistoryAnysis;
            hisController.sourceData = arr;
        }else if (index == 1)
        {
            NSArray * arr = remmenberDuplicate ? [manager getCreatOidList] : [self arrWithDelipublic:[manager getCreatOidList]];
            hisController.type = OIDHistoryCreat;
            hisController.sourceData = arr;

        }else
        {
//            NSArray * arr = [manager get];
            hisController.type = OIDHistoryCollect;
//            hisController.sourceData = arr;
        }
        [self.navigationController pushViewController:hisController animated:YES];
    }else if (groupIndex == 1)
    {
        if (index == 0)
        {
            SystemSetViewController * setttingController = [[SystemSetViewController alloc]init];
            [self.navigationController pushViewController:setttingController animated:YES];
        }else if (index == 1)
        {
            ContactUsViewController *contactUs = [[ContactUsViewController alloc]init];
            [self.navigationController pushViewController:contactUs animated:YES];
        }else if (index == 2)
        {
            
        }else
        {
            
        }
    }else
    {
        if (index == 0)
        {
            IntroduceOrgViewController * introduceController = [[IntroduceOrgViewController alloc]init];
            [self.navigationController pushViewController:introduceController animated:YES];
        }
    }
}

- (NSArray *)arrWithDelipublic:(NSArray *)sourceData{
    NSMutableArray *returnArr=[[NSMutableArray alloc]init];
    NSMutableSet * urlArr = @[].mutableCopy;
    for (NSDictionary * dict in sourceData) {
        NSString * url = [dict stringValueForKey:@"url"];
        if (![urlArr containsObject:url]){
            [urlArr addObject:url];
            [returnArr addObject:dict];
        }
    }
    return returnArr;
}

- (void)logoutAction{
    [MFWSdk accountLogout];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)dealloc{
    NSLog(@"dealloc");
}

- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
